import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import java.awt.Rectangle;

/**
 * GPanelHandler handles a GPanel object and sends appropriate commands/messages
 * to its GPanel object
 *
 * @version 1.0
 * @author P.Jaya Krishna
 * @author K.Vamsi Krishna
 */

class GPanelHandler extends MouseInputAdapter
{
	// refers to the GPanel object being handled
	private GPanel gPanel;
	private int ButtonDown;

	/**
	 * Creates a GPanelHandler object to handle the specified GPanel object.
	 */
	GPanelHandler(GPanel gPanel)
	{
		this.gPanel = gPanel;
		ButtonDown = 0;
	}

	// used for showing location
	// used for performing various graph operations
	public void mousePressed(MouseEvent e)
	{
		// status: show the mouse location
		gPanel.gFrame.statusBar.setMessage(GStatusBar.STATUS_LOCATION, ""+e.getX()+", "+e.getY());
		if(e.getButton() == MouseEvent.BUTTON1)
			ButtonDown = 1;
		else if(e.getButton() == MouseEvent.BUTTON2)
			ButtonDown = 2;
		else
			ButtonDown = 3;

		boolean flag = gPanel.checkVertex(e.getX(), e.getY());
		
		// to add vertex
		if(flag == false && gPanel.getCurrentAction() == GPanel.ACTION_VERTEX && e.getButton() == MouseEvent.BUTTON1)
			gPanel.doAddVertex(e.getX(), e.getY());

		// to remove vertex
		else if(flag == true && gPanel.getCurrentAction() == GPanel.ACTION_VERTEX && e.getButton() == MouseEvent.BUTTON3)
			gPanel.doRemoveVertex();

		// to show vertex popup
		else if(flag == true && gPanel.getCurrentAction() == GPanel.ACTION_SELECT && e.getButton() == MouseEvent.BUTTON3)
			new GVertexPopup(gPanel, e.getX(), e.getY());
		
		
		else if(flag == false && gPanel.getCurrentAction() == GPanel.ACTION_SELECT && e.getButton() == MouseEvent.BUTTON3)
		{
			// to show edge popup
			if( gPanel.checkEdge(e.getX(), e.getY()) == true)
				new GEdgePopup(gPanel, e.getX(), e.getY());
			// to show GPanel popup
			else
				new GPanelPopup(gPanel, e.getX(), e.getY());				
		}
		
		// to remove/add edge
		else if(flag == true && gPanel.getCurrentAction() == GPanel.ACTION_EDGE && e.getButton() == MouseEvent.BUTTON3)
		{
			try
			{
				// edge exists, delete it
				if(gPanel.getGraph().edgeExists(gPanel.getPrevVertex(), gPanel.getCurrVertex()) == true)
					gPanel.doRemoveEdge();
				// edge doesnot exist, add it
				else
					gPanel.doAddEdge();
			}
			catch(IllegalVertexException ee)
			{
				// No problem even if failed here
			}
		}
		gPanel.repaint();
	}
	
	// used for showing location
	// used for moving vertex
	// also used for increasing the size of the panel
	// panel width and/or height is increased with an increment of 10
	public void mouseDragged(MouseEvent e)
	{
		gPanel.gFrame.statusBar.setMessage(GStatusBar.STATUS_LOCATION, ""+e.getX()+", "+e.getY());
				
		if(ButtonDown == 1)
		{
			if(gPanel.getCurrentAction() != GPanel.ACTION_RUN)
				gPanel.doMoveVertex(e.getX(), e.getY());
	
			if(gPanel.getCurrVertex() != -1)
			{
				if(e.getX() > gPanel.getWidth() || e.getY() > gPanel.getHeight())
				{
					int newWidth = Math.max(gPanel.getWidth(), e.getX() + 10);
					int newHeight = Math.max(gPanel.getHeight(), e.getY() + 10);
					gPanel.setPreferredSize(new Dimension(newWidth, newHeight));
					gPanel.revalidate();
				}

				gPanel.scrollRectToVisible(new Rectangle(e.getX()-10, e.getY()-10, 25, 25));
				gPanel.repaint();
			}
		}
	}
	
	// used for showing location
	public void mouseMoved(MouseEvent e)
	{
		gPanel.gFrame.statusBar.setMessage(GStatusBar.STATUS_LOCATION, ""+e.getX()+", "+e.getY());		
	}
}